/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interfaz;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author Administrador
 */
public class OperacionesInterfaz {

    public static DefaultTreeModel treeFinal(String directory, boolean isServer, BufferedReader input,
            PrintWriter output) {
        if (!directory.equals("")) {
            DefaultMutableTreeNode father = new DefaultMutableTreeNode(directory);
            removeNodes(father, directory, isServer, input, output);
            JTree tree = new JTree(father);
            DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
            return model;
        } else {
            return null;
        }
    }

    public static boolean removeNodes(DefaultMutableTreeNode node, String directoryPath, boolean isServer,
            BufferedReader input, PrintWriter output) {
        node.removeAllChildren();
        return removeNodes(node, directoryPath, 2, isServer, input, output);
    }

    public static boolean removeNodes(DefaultMutableTreeNode node, String directoryPath, int index, boolean isServer,
            BufferedReader input, PrintWriter output) {
        ArrayList<String> list;
        if (isServer) {
            list = createArrayListFromServer(directoryPath, input, output);
        } else {
            list = createArrayList(directoryPath);
        }
        if (index > 0) {
            for (int i = 0; i < list.size(); i++) {

                DefaultMutableTreeNode children = new DefaultMutableTreeNode(list.get(i));
                removeNodes(children, list.get(i), index - 1, isServer, input, output);
                node.add(children);
            }


        }
        return true;
    }

    public static ArrayList<String> createArrayListFromServer(String filePath, BufferedReader input,
            PrintWriter output) {
        ArrayList<String> list = new ArrayList<>();
        output.println("listarServidor");
        output.flush();
        output.println(filePath);
        output.flush();
        String line;
        try {
            while (!(line = input.readLine()).equals("finDeLista")) {
                list.add(line);
            }
        } catch (Exception e) {
        }
        return list;
    }

    public static ArrayList<String> createArrayList(String filePath) {
        ArrayList<String> list = new ArrayList<>();
        File f = new File(filePath);
        File[] fileList = f.listFiles();
        if (fileList != null) {
            for (int i = 0; i < fileList.length; i++) {
                if (fileList[i].isDirectory()) {
                    list.add(fileList[i].toString());
                }
            }
        }
        return list;
    }

    public static boolean treeExpanded(TreeExpansionEvent evt, boolean isServer, BufferedReader input,
            PrintWriter output) {
        TreePath directory = evt.getPath();
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) directory.getLastPathComponent();
        if (node == null) {
            return true;
        }
        removeNodes(node, (String) node.getUserObject(), isServer, input, output);
        JTree tree = (JTree) evt.getSource();
        DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
        model.nodeStructureChanged(node);
        return false;
    }

    public static String mostrarArchivosLocal(TreeSelectionEvent evento, JTable tabla) {

        DefaultTableModel modeloTabla;
        TreePath path = evento.getPath();
        DefaultMutableTreeNode nodo = (DefaultMutableTreeNode) path.getLastPathComponent();
        File carpeta = new File(nodo.toString());
        File[] listaFicheros = carpeta.listFiles();
        modeloTabla = (DefaultTableModel) tabla.getModel();
        modeloTabla.setNumRows(0);

        for (int i = 0; i < listaFicheros.length; i++) {

            if (listaFicheros[i].isFile()) {

                Date fecha = new Date(listaFicheros[i].lastModified());
                Object[] datosFichero = new Object[5];
                datosFichero[0] = listaFicheros[i].getName();
                datosFichero[1] = listaFicheros[i].length() + " Bytes";
                datosFichero[2] = fecha.toString();
                datosFichero[3] = mirarPermisos(listaFicheros[i]);
                datosFichero[4] = listaFicheros[i].isHidden();
                modeloTabla.addRow(datosFichero);
            }

        }
        return (nodo.toString());
    }

    public static String mostrarArchivosServidor(TreeSelectionEvent evento, JTable tabla, BufferedReader entrada, PrintWriter salida) {

        DefaultTableModel modeloTabla;
        TreePath path = evento.getPath();
        DefaultMutableTreeNode nodo = (DefaultMutableTreeNode) path.getLastPathComponent();
        modeloTabla = (DefaultTableModel) tabla.getModel();
        modeloTabla.setNumRows(0);
        salida.println("listarFicheros");
        salida.flush();
        System.out.println(nodo.toString());
        salida.println(nodo.toString());
        salida.flush();
        boolean hola = true;
        String linea;
        try {
            while (hola) {
                linea = entrada.readLine();
                if (linea.equals("finDeLista")) {
                    hola = false;
                }else{                    
                    Object[] datosFichero = new Object[5];
                    datosFichero[0] = linea;
                    datosFichero[1] = entrada.readLine();
                    datosFichero[2] = entrada.readLine();
                    datosFichero[3] = entrada.readLine();   
                    datosFichero[4] = entrada.readLine();
                    modeloTabla.addRow(datosFichero);
                    
                }
            }
            }catch(Exception e){
                e.printStackTrace();
        }
        return (nodo.toString());

    }

    public static String mirarPermisos(File fichero) {

        String permisos = "";

        if (fichero.canRead()) {
            permisos = "Lectura";
        }
        if (fichero.canWrite()) {
            permisos = permisos + " Escritura";
        }
        if (fichero.canExecute()) {
            permisos = permisos + " Ejecutar";
        }

        return permisos;

    }
}